//导入需要使用模块
import http from "@/api/http";
import { 
    nowPlayingListUri, 
    comingSoonListUri, 
    moiveDetailUrl, 
    cityListUrl, 
    cinemaListUrl,
    loginUrl,
    centerUrl, 
} from "@/config/url";
//请求正在热映列表数据
export const nowPlayingListData = (pageNum: string) => {
    return http.get(nowPlayingListUri + pageNum, {
        headers: {
            info: "",
            authorization: ""
        }
    });
};
//请求即将上映列表数据
export const comingSoonListData = (pageNum: string) => {
    return http.get(comingSoonListUri + pageNum);
};
//请求电影详情数据
export const movieDetailData = (filmId: string) => {
    //给axios设置请求头
    return http.get(moiveDetailUrl + filmId, {
        headers: {
            info: "info",
            authorization: ""
        }
    });
};
// 城市列表数据
export const cityListData = async () => {
    // 给axios设置请求头
    const ret = await http.get(cityListUrl, {
        headers: {
            info: "city",
            authorization: ""
        }
    });
    // 定义基本的数据
    const cities = ret.data.cities;
    const codeList: string[] = []; //'A','B','C'....（完整26个字母）
    const dataList: object[] = []; // 城市信息
    const indexList: string[] = []; //'A','B','C'....（经过筛选的字母）

    // for循环生成26个字母
    for (let i = 65; i <= 90; i++) {
        codeList.push(String.fromCharCode(i));
    }
    // 开始处理
    codeList.forEach((element) => {
        // 与城市信息的pinyin字段首字母进行对比，如果符合则留下大字母
        const tempArr = cities.filter(
            (item: {
                pinyin: string
            }) => element.toLowerCase() == item.pinyin.slice(0, 1)
        );
        if (tempArr.length > 0) {
            indexList.push(element);
            dataList.push({
                index: element,
                data: tempArr,
            });
        }
    });
    console.log(dataList);
    // 返回数据
    return Promise.resolve([dataList, indexList]);
};
// 请求所在城市的电影院列表
export const getCinemasList = (cityId: string) => {
    // 给axios设置请求头
    return http.get(cinemaListUrl + cityId, {
        headers: {
            info: "cinema",
            authorization: ""
        }
    });
};
// 登录
export const userLogin = (data: object) => {
    return http.post(loginUrl, data);
};
// 获取用户个人信息
export const userInfo = (_token: string) => {
    return http.get(centerUrl, {
        headers: {
            info: "",
            authorization: _token
        }
    });
};